package entities;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="referee")
@DiscriminatorValue("referee")
@NamedQueries({
	@NamedQuery(name="getListRefereeByFlagActivatedByTournament",query="SELECT referee FROM Referee AS referee WHERE referee.oneFlag = :flag AND referee.tournament = :tournament"),
	@NamedQuery(name="getListRefereeByFlagActivated",query="SELECT referee FROM Referee AS referee WHERE referee.oneFlag = :flag")
})
public class Referee extends User implements Serializable {

	private static final long serialVersionUID = 1L;

	@OneToMany(fetch = EAGER, cascade = ALL, mappedBy = "referee")
	private Set<Match> listMatchs;

	@ManyToOne
	private Tournament tournament;

	public Set<Match> getListMatchs() {
		return listMatchs;
	}

	public void setListMatchs(Set<Match> listMatchs) {
		this.listMatchs = listMatchs;
	}

	public Tournament getTournament() {
		return tournament;
	}

	public void setTournament(Tournament tournament) {
		this.tournament = tournament;
	}
	
	@Override
	public String toString() {
		return getLastName() + " " + getFirstName();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((tournament == null) ? 0 : tournament.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Referee other = (Referee) obj;
		if (tournament == null) {
			if (other.tournament != null)
				return false;
		} else if (!tournament.equals(other.tournament))
			return false;
		return true;
	}
}